package com.sinosoft.dsp.platform.components;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.to.IGenericTO;

/**
 * 这是一个复合的视图类，它能够提供视图类的列表集合
 * 
 * @since 1.0 2010-3-25
 * @author 潘巍（Peter Pan）
 * 
 */
public class ViewListBean extends AbstractViewBean implements Serializable,
		IViewCompsiteBean {

	private static final long serialVersionUID = 3728400889319174900L;

	private static final Log log = Log.getLog(ViewListBean.class.getName());

	private List<IViewBean> views = new ArrayList<IViewBean>();

	@Override
	public boolean addView(IViewBean vb) {
		if (indexOf(vb.getId()) < 0) {
			views.add(vb);
			return true;
		}
		return false;
	}

	@Override
	public boolean addView(int index, IViewBean vb) {
		try {
			views.add(index, vb);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public int indexOf(String id) {
		for (int i = 0; i < views.size(); i++) {
			if (views.get(i).getId().equals(id)) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public IViewBean getView(int i) {
		try {
			return this.views.get(i);
		} catch (Exception e) {
			log.error("通过序号获得子ViewBean出现错误，ID：" + getId() + "，序号：" + i);
		}
		return null;
	}

	@Override
	public boolean removeView(String id) {
		return removeView(indexOf(id)) == null ? false : true;
	}

	@Override
	public IViewBean removeView(int index) {
		return views.remove(index);
	}

	@Override
	public int getSize() {
		return views.size();
	}

	@Override
	public List<IViewBean> getAllView() {
		return views;
	}

	@Override
	public void init(IGenericTO tos) throws SinoException {
		for (IViewBean v : views) {
			v.init(tos);
		}
	}

	@Override
	public void destroy() {
		super.destroy();
		for (IViewBean v : views) {
			v.destroy();
		}
	}

}
